﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using ZLDC.Common.Estate;

namespace ZLDC.DAL.Estate
{
    public class HuosingPicDAL
    {
                public static void p_huosing_pic_insert(string picurl, string picurlSmall, string name, int type, int hid, string hestatename, string hroomno, string remark, int dltFlag,string deptid,string dept,string userid,string user,out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@picurl", picurl));
            parms.Add(new SqlParameter("@picurlSmall", picurlSmall));
            parms.Add(new SqlParameter("@name", name));
            parms.Add(new SqlParameter("@type", type));
            parms.Add(new SqlParameter("@hid", hid));
            parms.Add(new SqlParameter("@hestatename", hestatename));
            parms.Add(new SqlParameter("@hroomno", hroomno));
            parms.Add(new SqlParameter("@remark", remark));
            parms.Add(new SqlParameter("@dltFlag", dltFlag));
            parms.Add(new SqlParameter("@deptid",deptid));
            parms.Add(new SqlParameter("@dept",dept));
            parms.Add(new SqlParameter("@userid",userid));
            parms.Add(new SqlParameter("@user", user));


            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);
            db.execute_procedure("p_housing_pic_insert", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

        }
                                                                public static DataTable p_huosing_pic_listall_table(int id, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@id", id));
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);

            DataTable dt = null;
            dt = db.GetDataTable("p_housing_pic_listall", parms);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            return dt;
        }
                public static int DelPic(int id, string deptid, _Database db)
        {
            int rows = 0;
            string sql;
            try
            {
                if (ZLDC.Common.BaseUser.UserInfo.GetPopedom("删除图片")==true)                {
                    sql = "update t_housing_pic set dltflag=1  where id=" + id + "";
                }
                else
                {
                    string IdList = ZLDC.Common.BaseUser.UserInfo.getAllChildDept(_Utility.GetInfoBySession().m_sDeptID, _Utility.GetInfoBySession().m_sUserID);
                    sql = "update t_housing_pic set dltflag=1  where id=" + id + " and deptid in(" + IdList + ")";
                }
                rows = db.ExecuteSql(sql);
            }
            catch
            {

            }
            finally
            {
                db.close();
            }
            return rows;
        }

        public static DataTable p_housing_pic_list_table(int page, int pagesize, string condition, ref int total, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@page", page));
            parms.Add(new SqlParameter("@pagesize", pagesize));
            parms.Add(new SqlParameter("@condition", condition));

            SqlParameter pTotal = new SqlParameter("@total", SqlDbType.Int, 4);
            pTotal.Direction = ParameterDirection.InputOutput;
            pTotal.Value = total;
            parms.Add(pTotal);
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);

            DataTable dt = null;
            dt = db.GetDataTable("p_housing_pic_list", parms);

            total = (pTotal.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pTotal.Value);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            return dt;
        }

                                                        public static int IsExitHousingPicToday(int id,_Database db)
        {
            int count = 0;

            try
            {
                string Sql = "select count(*) FROM t_housing_pic where hid=" + id + " and DATEDIFF(DAY,createDate,GETDATE())=0 and userid=" + _Utility.GetInfoBySession().m_sUserID + "";

                DataSet ds = db.Query(Sql);

                if (ds.Tables[0].Rows.Count > 0)
                    count = _Convert.ToInt(ds.Tables[0].Rows[0][0].ToString());
            }
            catch
            {

            }
            finally
            {
                db.close();
            }
            return count;
        }
    }
}
